/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.gui;

import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;

import org.openeccos.EccosAppInstance;
import org.openeccos.EccosDesktop;
import org.openeccos.EccosUserSession;
import org.openeccos.dao.EccosRepository;
import org.openeccos.dao.EccosService;
import org.openeccos.model.MUser;
import org.openeccos.util.PDUtil;
import org.openeccos.widgets.PDLabel;
import org.openeccos.widgets.PDTextField;

import com.sas.framework.expojo.ModelExposer;

import echopointng.PasswordFieldEx;

/**
 * A wizard where a user can register to the system and/or complete his registration
 *
 * @author cgspinner@web.de
 */
public class WzdNewUser extends PDWizard {

	protected MUser user;

	public WzdNewUser() {
		this(new MUser());
	}

	public WzdNewUser(MUser user) {
		super(null);
		this.user = user;
		setHeight(new Extent(450));
		setTitle("Anmeldung neuer Benutzer");
		init2();
	}
	
	protected void init2() {
		addPanel(new Panel1());
		addPanel(new Panel2());
		addPanel(new Panel3());
		addPanel(new Panel4());
		showPage(true);
	}

	protected class Panel1 extends PDWizardPanel {
		public Panel1() {
			super("Willkommen!", null, "Weiter");

			String s = "Bitte folgen Sie den nächsten Schritten, um sich im System zu registrieren.";
			PDLabel lblWelcome = new PDLabel(s, PDLabel.BORDERED);
			lblWelcome.setWidth(new Extent(90, Extent.PERCENT));
			lblWelcome.setLineWrap(true);
			lblWelcome.setInsets(new Insets(8));
			add(lblWelcome);
		}
	}

	protected class Panel2 extends PDWizardPanel {
		private PnlUser pnlUser;

		public Panel2() {
			super("Persönliche Daten", "Zurück", "Weiter");

			pnlUser = new PnlUser(user);
			pnlUser.setWidth(new Extent(92, Extent.PERCENT));
			add(pnlUser);
		}

		@Override
		public void applyToModel() {
			pnlUser.saveToModel();
		}
	}

	protected class Panel3 extends PDWizardPanel {

		private Grid grid;
		private PDTextField txtLogin;
		private PasswordFieldEx txtPassword1;
		private PasswordFieldEx txtPassword2;

		public Panel3() {
			super("Login", "Zurück", "Weiter");

			String s = "Bitte wählen Sie hier ein Login und ein Passwort, mit welchem Sie sich in Zukunft anmelden können";
			PDLabel lblInfo = new PDLabel(s, PDLabel.BORDERED);
			lblInfo.setWidth(new Extent(90, Extent.PERCENT));
			lblInfo.setLineWrap(true);
			lblInfo.setInsets(new Insets(8));
			add(lblInfo);

			grid = new Grid(2);
			grid.setInsets(new Insets(3));
			add(grid);

			grid.add(new PDLabel("Login", PDLabel.FIELD_LABEL));
			txtLogin = new PDTextField();
			grid.add(txtLogin);

			grid.add(new PDLabel("Passwort", PDLabel.FIELD_LABEL));
			txtPassword1 = new PasswordFieldEx();
			grid.add(txtPassword1);

			grid.add(new PDLabel("Passwort (wiederholen", PDLabel.FIELD_LABEL));
			txtPassword2 = new PasswordFieldEx();
			grid.add(txtPassword2);
		}

		@Override
		public void applyToModel() {
			user.setLogin(txtLogin.getText());
			user.setPassword(txtPassword1.getText().trim());
		}

		@Override
		public String getError() {
			if (PDUtil.isEmpty(txtLogin.getText())) {
				return "Bitte geben Sie ein Login an!";
			}
			EccosRepository repo = (EccosRepository)ModelExposer.get().getRepository(EccosRepository.NAME);
			if (repo.loginExist(txtLogin.getText())) {
				return "Login bereits vergeben, bitte wählen Sie ein anderes!";
			}
			if (PDUtil.isEmpty(txtPassword1.getText())) {
				return "Bitte geben Sie ein Passwort an!";
			}
			if (!txtPassword1.getText().equals(txtPassword2.getText())) {
				return "Passwörter sind nicht gleich!";
			}
			return null;
		}
	}

	protected class Panel4 extends PDWizardPanel {

		public Panel4() {
			super("Fertig", null, "Fertig");

			String s = "Herzlichen Glückwunsch, Sie haben sich erfolgreich angemeldet";
			PDLabel lblInfo = new PDLabel(s, PDLabel.BORDERED);
			lblInfo.setWidth(new Extent(90, Extent.PERCENT));
			lblInfo.setLineWrap(true);
			lblInfo.setInsets(new Insets(8));
			add(lblInfo);
		}

		@Override
		public void readFromModel() {
			EccosService service = (EccosService)ModelExposer.get().getService(EccosService.NAME);
			service.persist(user);
		}

		@Override
		public void applyToModel() {
			EccosUserSession us = ((EccosAppInstance)EccosDesktop.getDesktop().getApplicationInstance()).getUserSession();
			us.doLogin(user.getLogin(), user.getPassword());
			EccosDesktop.getDesktop().updateLoginState();
		}
	}
}